package com.springcloud.sc.ucpv5.client.module.datamanager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import com.springcloud.sc.comm.core.constant.CommonConstants;
import com.springcloud.sc.ucp.v5.api.feign.UcpRemoteUserService;
import com.springcloud.sc.ucp.v5.api.vo.UserVO;
import com.springcloud.sc.ucpv5.client.application.message.DataMessageManager;
import com.springcloud.sc.ucpv5.client.application.message.MessageService;
import com.springcloud.sc.ucpv5.client.commondata.BaseListModel;
import com.springcloud.sc.ucpv5.client.commondata.MyGroup;
import com.springcloud.sc.ucpv5.client.commondata.MyNode;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.commondata.RootModel;
import com.springcloud.sc.ucpv5.client.commondata.SecuResult;
import com.springcloud.sc.ucpv5.client.commondata.data.Group;
import com.springcloud.sc.ucpv5.client.commondata.data.GroupQueryRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.Node;
import com.springcloud.sc.ucpv5.client.commondata.data.NodeQueryRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.User;
import com.springcloud.sc.ucpv5.client.commondata.data.UserInfo;
import com.springcloud.sc.ucpv5.client.util.ThreadUtil;
import com.springcloud.sc.ucpv5.tree.GroupNode;
import com.springcloud.sc.ucpv5.tree.InfoTree;
import com.springcloud.sc.ucpv5.tree.data.WebTreeNode;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

@Data
public class BaseDataManager {

	@Autowired
	private UcpRemoteUserService ucpRemoteUserService;

	private static BaseDataManager instance;
	private Map<String, MyNode> nodeMap = Collections.synchronizedMap(new HashMap<String, MyNode>());
	private Map<String, MyGroup> groupMap = Collections.synchronizedMap(new HashMap<String, MyGroup>());
	private Map<String, MyUser> userMap = Collections.synchronizedMap(new HashMap<String, MyUser>());
	private static final Logger logger = LoggerFactory.getLogger(BaseDataManager.class);

	private Map<String,MyNode> nodeTreeMap = Collections.synchronizedMap(new HashMap<>());
	private Map<String,MyGroup> groupTreeMap = Collections.synchronizedMap(new HashMap<>());
	private List<InfoTree> infoTreeList = Collections.synchronizedList(new ArrayList<>());

	//通过定时任务的方式，把数据加载
	private List<GroupNode> groupNodeList =  Collections.synchronizedList(new ArrayList<>());

	public List getGroupNodeList(){
		return this.groupNodeList;
	}


	private BaseDataManager(){ }

	public static BaseDataManager getInstance() {
		if (instance == null) {
			instance = new BaseDataManager();
		}
		return instance;
	}

	@PostConstruct
	public void init(){
		instance = this;
		instance.ucpRemoteUserService = this.ucpRemoteUserService;
	}

/*	public static BaseDataManager getInstance() {
		if (instance == null) {
			instance = new BaseDataManager();
		}
		return instance;
	}*/


	public MyNode nodeGet(String number) {
		if (number == null || number.equals("")) {
			// 因为V4中, 操作当前节点是下发""
			return this.loginNode;
		}

		MyNode data = nodeMap.get(number);
		return data;
	}
	public MyNode nodeAdd(MyNode node) {
		MyNode node_ = nodeMap.get(node.getLogic_NodeNumber());
		if (node_ != null) {
			// 是否需要更新数据
			// System.out.println("节点已经存在了, 不能再增加!!!!!!!!:" + node.getLogic_NodeNumber());
		} else {
			nodeMap.put(node.getLogic_NodeNumber(), node);
			node_ = node;
			nodeTreeMap.put( node.getIdlData().getNodeNumber() , node  );
		}
		return node_;
	}

	public Map<String,MyNode> getNodeMap(){
		return nodeTreeMap;
	}

	public Map getGroupTreeMap(){
		return groupTreeMap;
	}

	/*public InfoTree getNodeInfoTree(MyNode myNode , String parentId){
		InfoTree infoTree = null;
		infoTree =  new InfoTree();
		infoTree.setId( myNode.getIdlData().NodeNumber );
		infoTree.setParentId( parentId );
		infoTree.setType("node");
		infoTree.setName( myNode.getIdlData().NodeName );
		infoTree.setNodeNumber( myNode.getIdlData().NodeNumber );
		infoTree.setPhysicsNodeNumber( myNode.getIdlData().Physics_NodeNumber );
		infoTree.setTypeChild( myNode.getNodeType() );
		return infoTree;
	}

	public InfoTree getGroupInfoTree(MyGroup myGroup  ){

		InfoTree infoTree = null;
		Group group = myGroup.getIdlData();
		infoTree =  new InfoTree();
		infoTree.setId( myGroup.getTelno() );
		infoTree.setParentId(  myGroup.getIdlData().getParGrpTelno() );
		infoTree.setType("group");
		int userNum =  myGroup.getListModel().getUserList().size();
		infoTree.setName( group.GroupName+"("+group.getGroupTelno()+")("+userNum+")" );
		infoTree.setNodeNumber( myGroup.getLogicNodeNumber() );
		infoTree.setPhysicsNodeNumber( myGroup.getPhysicsNodeNumber() );
		infoTree.setTypeChild( myGroup.getIdlData().getGroupType() );
		return infoTree;

	}


	public InfoTree getMyUserTree(MyUser myUser){

		InfoTree infoTree = null;
		User  user = myUser.getIdlData();
		infoTree =  new InfoTree();
		infoTree.setId( user.getUserTelno() );
		infoTree.setParentId(  user.getGrpList() );
		infoTree.setType("user");
		infoTree.setName( user.getUserName()+"("+user.getUserTelno()+")" );
		infoTree.setNodeNumber( user.getUserTelno() );
		infoTree.setPhysicsNodeNumber( myUser.getPhysicsNodeNumber() );
		infoTree.setPriority( user.getPriority() );
		int userType =  user.getUserType();
		int userTypeData = userType & 0x000000FF;
		int tmp = (userType >> 10) & 0x0000000F;
		userType = userTypeData * 10 + tmp;
		infoTree.setTypeChild( userType );
		return infoTree;

	}*/


	public MyNode nodePushData(Node nodeRsp, boolean isRmv) {
		MyNode node = nodeGet(nodeRsp.NodeNumber);
		MyNode parentNode = nodeGet( nodeRsp.ParentNumber );

		if (parentNode == null) {
			return null;
		}

		if (node == null) {
			node = new MyNode(nodeRsp);
		}

		if (isRmv) {
			if (node == null) {
				node = new MyNode(nodeRsp);
			}
			parentNode.getListModel().removeNode(node, true);
			return null;
		} else {
			if (node == null) {
				// 增加
				// 加入节点
				parentNode.getListModel().addNode(node, true);
			} else {
				node.setIdlData(nodeRsp);
			}
		}

		return node;
	}

	public void nodeRmv(MyNode data) {
		nodeMap.remove(data.getLogic_NodeNumber());
	}

	public MyGroup groupGet(String number) {
		MyGroup data = groupMap.get(number);
		return data;
	}

	public MyGroup groupAdd(MyGroup group) {
		MyGroup group_ = groupMap.get(group.getTelno());
		if (group_ != null) {
			// 是否需要更新数据
			System.out.println("组已经存在了, 不能再增加!!!!!!!!:" + group.getTelno());
		} else {
			groupMap.put(group.getTelno(), group);
			group_ = group;
			groupTreeMap.put( group.getIdlData().getGroupTelno() , group  );
			// 增加是为了查询组内用户出来
			// ThreadUtil.getInstance().addGroupNum(group.getTelno());
		}
		return group_;
	}

	public List<MyGroup> groupGetAll() {
		try {
			ArrayList<MyGroup> tmpGroupList = new ArrayList<MyGroup>();
			for (MyGroup group : groupMap.values()) {
				// 如果该组是临时组,且组号码包含当前登陆号码(即属于本机建立的,则显示)
				tmpGroupList.add(group);
			}
			return tmpGroupList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 取得所有临时组列表
	 * 
	 * @return
	 */
	public List<MyGroup> groupGet_TmpGroup() {
		try {
			ArrayList<MyGroup> tmpGroupList = new ArrayList<MyGroup>();
			for (MyGroup group : groupMap.values()) {
				// 如果该组是临时组,且组号码包含当前登陆号码(即属于本机建立的,则显示)
				if (group.isTmpGroup()) {
					tmpGroupList.add(group);
				}
			}
			return tmpGroupList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public MyGroup groupPushData(MyNode node, Group groupRsp, boolean isRmv) {
		MyGroup group = BaseDataManager.getInstance().groupGet(groupRsp.GroupTelno);
		MyGroup parentGroup = BaseDataManager.getInstance().groupGet(groupRsp.ParGrpTelno);

		BaseListModel baseListModel = null;
		if (parentGroup == null) {
			if (node.getLogic_NodeNumber().equals(groupRsp.ParGrpTelno)) {
				baseListModel = node.getListModel();
			}
		} else {
			baseListModel = parentGroup.getListModel();
		}

		if (baseListModel == null) {
			return null;
		}

		if (isRmv) {
			// 删除组
			if (group == null) {
				group = new MyGroup(groupRsp, node.getPhysicsNodeNumber(), node.getLogic_NodeNumber(), 0);
			}
			baseListModel.removeGroup(group, true);
			groupRmv(group);
			return null;
		} else {
			if (group == null) {
				group = new MyGroup(groupRsp, node.getPhysicsNodeNumber(), node.getLogic_NodeNumber(), 0);
			} else {
				// 增加
				group.setIdlData(groupRsp);
			}
			baseListModel.addGroup(group, true);
		}

		return group;
	}
    /**
     * 更新单组信息
     * @param groupRsp
     * @return
     */
    public void groupPushData(String PhysicsNodeNumber, String nodeNumberm, GroupQueryRsp groupRsp) {
	// 解析组数据
	if (groupRsp != null) {
	    for (int i = 0; i < groupRsp.Rows.length; ++i) {
		MyGroup group = groupMap.get(groupRsp.Rows[i].GroupTelno);

		if (group == null) {
		    ;
		} else {
		    group.setIdlData(groupRsp.Rows[i]);
		    // group.getListModel().clear(); // 清除组中的关系表
		}
	    }
	}
    }
	public void groupRmv(MyGroup group) {
		groupMap.remove(group.getTelno());
	}

	public MyUser userGet(String number) {
		MyUser data = userMap.get(number);
		return data;
	}

	public ArrayList<MyUser> userGetAll() {
		try {
			ArrayList<MyUser> tmpGroupList = new ArrayList<MyUser>();
			for (MyUser group : userMap.values()) {
				// 如果该组是临时组,且组号码包含当前登陆号码(即属于本机建立的,则显示)
				tmpGroupList.add(group);
			}
			return tmpGroupList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public MyUser userAdd(MyUser user) {
		MyUser user_ = userMap.get(user.getUserTelNo());
		if (user_ != null) {
			// 是否需要更新数据
			System.out.println("用户已经存在了, 不能再增加!!!!!!!!:" + user.getUserTelNo());
		} else {
			userMap.put(user.getUserTelNo(), user);
			user_ = user;

			// wsy 20190607 处理如果是调度员, 同步到web管理员中
			if(false){
				if(user.isDispather()){
					String logicNodeNumber = "100";
					//保存用户信息至WEB系统
					UserVO userVO = new UserVO();
					userVO.setUserId( user.getUserInfo().getUserTelno() );
					userVO.setUsername( user.getUserInfo().getUserTelno() );
					userVO.setPassword( user.getUserInfo().getPassword() );
					userVO.setDeptId( logicNodeNumber );
					userVO.setRoleId( user.getUserInfo().getGrpList() );
					userVO.setSystemId( CommonConstants.STATUS_NORMAL );
					boolean bool =  ucpRemoteUserService.saveUserAPI(  userVO );
				}
			}
		}
		return user_;
	}

	public MyUser userPushData(String physicsNodeNumber, String logicNodeNumber, User user_get, boolean isRmv) {
		MyNode node = nodeGet(logicNodeNumber);
		if (node != null) {
			return userPushData(node, user_get, isRmv);
		} else {
			MyUser user = userMap.get(user_get.UserTelno);
			if (isRmv) {
				// 删除用户处理
				if (user == null) {
					return null;
				} else {
					// 处理用户加入组的信息
					userDealGroupList(node, user, false);
					userMap.remove(user_get.UserTelno);
				}
			} else {
				if (user != null) {
					// 用户所属的组,采用叠加方式
					String groupList = user.getIdlData().GrpList;
					String groupList_new = user_get.GrpList;
					user.setIdlData(user_get);
					user.getIdlData().GrpList = groupList;
					user.dealToGroup(groupList_new, true);

					user.setPhysicsNodeNumber(physicsNodeNumber);
					user.setLogicNodeNumber(logicNodeNumber);
				} else {
					user = new MyUser(user_get, physicsNodeNumber, logicNodeNumber, 0);
					userMap.put(user_get.UserTelno, user);
				}

				// 处理用户加入组的信息
				userDealGroupList(node, user, true);
			}
			return user;
		}
	}

	/**
	 * 统一处理User对象的更新
	 * 
	 * @param node
	 * @param user_get
	 * @return
	 */
	public MyUser userPushData(MyNode node, User user_get, boolean isRmv) {
		MyUser user = userMap.get(user_get.UserTelno);
		if (isRmv) {
			// 删除用户处理
			if (user == null) {
				return null;
			} else {
				// 处理用户加入组的信息
				userDealGroupList(node, user, false);
				userMap.remove(user_get.UserTelno);
			}
		} else {
			if (user != null) {
				// 用户所属的组,采用叠加方式
				String groupList = user.getIdlData().GrpList;
				String groupList_new = user_get.GrpList;
				user.setIdlData(user_get);
				user.getIdlData().GrpList = groupList;
				user.dealToGroup(groupList_new, true);

				user.setPhysicsNodeNumber(node.getPhysicsNodeNumber());
				user.setLogicNodeNumber(node.getLogic_NodeNumber());

			} else {
				user = new MyUser(user_get, node.getPhysicsNodeNumber(), node.getLogic_NodeNumber(), 0);
				userMap.put(user_get.UserTelno, user);
			}

			// 处理用户加入组的信息
			userDealGroupList(node, user, true);
		}
		return user;
	}

	public MyUser userPushData(MyNode node, UserInfo user_get) {
		MyUser user = userMap.get(user_get.UserTelno);
		if (user != null) {
			// 用户所属的组,采用叠加方式
			String groupList = user.getIdlData().GrpList;
			String groupList_new = user_get.GrpList;
			user.setUserInfo(user_get);
			user.getIdlData().GrpList = groupList;
			user.getUserInfo().GrpList = groupList;
			user.dealToGroup(groupList_new, true);

			user.setPhysicsNodeNumber(node.getPhysicsNodeNumber());
			user.setLogicNodeNumber(node.getLogic_NodeNumber());

		} else {
			user = new MyUser(user_get, node.getPhysicsNodeNumber(), node.getLogic_NodeNumber(), 0);
			userMap.put(user_get.UserTelno, user);
		}
		userDealGroupList(node, user, true);
		return user;
	}

	public void userDealGroupList(MyNode node, MyUser user, boolean isAdd) {
		// 处理用户加入组的信息
		ArrayList<MyGroup> list = user.getGroupList();
		if (isAdd) {
			if (list.size() == 0) {
				// 加入节点中
				node.getListModel().addUser(user, false);
			} else {
				for (MyGroup tmp : list) {
					if (tmp.getListModel().addUser(user, false)) {
						user.dealToGroup(tmp.getIdlData().GroupTelno, true);
					}
				}
			}
		} else {
			for (MyGroup tmp : list) {
				tmp.getListModel().removeUser(user, false);
			}
		}
	}

	public MyUser userGetByTelNum(String number) {
		MyUser data = userMap.get(number);
		return data;
	}

	public MyUser userGetByUserName(String userName) {
		for (MyUser user : userMap.values()) {
			if (user.getIdlData().Desc.equals(userName) || user.getIdlData().UserName.equals(userName)) {
				return user;
			}
		}
		return null;
	}

	/**
	 * 将所有用户的上传状态置非上传
	 * 
	 * @param node_tmp
	 */
	public void userUpdateAllUserMediaStatus(MyNode node_tmp) {
		for (MyUser user : userMap.values()) {
			if (user.isVideoUp()) {
				user.removeStatusFalg(MyUser.STATUS_VIDEO_UP);
			}
		}
	}

	private MyNode loginNode;

	public MyNode getLoginNode() {
		return loginNode;
	}

	private WebTreeNode localWebTreeNode;
	public WebTreeNode getLoginWebTreeNode(){
		return localWebTreeNode;
	}

	private RootModel rootModel = new RootModel();

	public void setRootModel(RootModel rootModel) {
		this.rootModel = rootModel;
	}

	public RootModel getRootModel() {
		return rootModel;
	}

	public void initLoginNode(String physicsNodeNumber, String logicNodeNumber) {
		try {
			MyNode loginNode_ = nodeGet(logicNodeNumber);
			if (loginNode_ == null) {
				loginNode_ = new MyNode(physicsNodeNumber, logicNodeNumber);
			}
			loginNode = loginNode_;
			// 20140918 调度台在注销后,登录别的节点号不一致的ucp,角色查询失败,原因角色查询使用currentNode
			currentNode = loginNode;

			// 出现节点列表中, 先前保存的节点号码改了, 但是map的key没有改
			nodeAdd(loginNode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private MyNode currentNode;

	public MyNode getCurrentNode() {
		if (currentNode == null) {
			return this.getLoginNode();
		}

		return currentNode;
	}

	public List<InfoTree> getCurrentInfoTree(){

		if(infoTreeList == null || infoTreeList.isEmpty()){
			MyNode node = getCurrentNode();
			InfoTree infoTree = new InfoTree();
			infoTree.setParentId("-1");
			infoTree.setType("node");
			infoTree.setTypeChild( node.getNodeType() );
			infoTree.setNodeNumber( node.getIdlData().getNodeNumber() );
			infoTreeList.add( infoTree );
		}
		return infoTreeList;

	}

	public void setCurrentNode(MyNode currentNode) {
		this.currentNode = currentNode;
	}

	// 20170728 设置保存当前选择的组信息
	private MyGroup currentGroup;

	public MyGroup getCurrentGroup() {
		return currentGroup;
	}

	public void setCurrentGroup(MyGroup currentGroup) {
		this.currentGroup = currentGroup;
	}

	public SecuResult nodeLoadData(MyNode node) {
		return nodeLoadData(node, false);
	}

	public void nodeLoadDataTree(MyNode node , InfoTree infoTree ){

	}

	/**
	 * notify 是在定义是否刷新界面
	 * 
	 * @param notify
	 * @return
	 */
	public SecuResult nodeLoadData(MyNode node, boolean notify) {
		try {
			SecuResult retResult = new SecuResult();
			{
				{
					SecuResult tmpresult = nodeLoadNodeData(node, false);
					retResult.uniteResult(tmpresult);
				}
				{
					SecuResult tmpresult = nodeLoadGroupData(node, false);
					retResult.uniteResult(tmpresult);
				}
				try {
					ThreadUtil.getInstance().加载子节点的节点数据(node);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			node.setIsInit(MyNode.INIT_TYPE_ONLINE);

			return retResult;
		} finally {
		}
	}

	public SecuResult nodeLoadNodeData(MyNode node, boolean notify) {
		logger.info("加载节点:" + node.getIdlData().NodeNumber);
		NodeQueryRsp nodeRsp = new NodeQueryRsp();
		SecuResult tmpresult = DataMessageManager.getInstance().nodeQueryNodeList(node.getPhysicsNodeNumber(), node.getLogic_NodeNumber(), nodeRsp, false, false);
		if (tmpresult.getRetCode() == 0) {
			nodePushData(node, nodeRsp);
		}
		return tmpresult;

	}

	public void nodePushData(MyNode nodeStub, NodeQueryRsp nodeRsp) {
		if (nodeRsp == null) {
			return;
		}
		for (int i = 0; i < nodeRsp.NodeList.length; ++i) {
			if (nodeRsp.NodeList[i] == null)
				continue;

			boolean isThisNode = false;
			if (nodeStub.getIdlData().Physics_NodeNumber.equals(nodeRsp.NodeList[i].Physics_NodeNumber) && nodeStub.getIdlData().NodeNumber.equals(nodeRsp.NodeList[i].NodeNumber)) {
				isThisNode = true;
			}
			MyNode node = nodeMap.get(nodeRsp.NodeList[i].NodeNumber);
			if (node == null) {
				if (isThisNode) {
					node = nodeStub;
					node.setIdlData(nodeRsp.NodeList[i]);
				} else {
					node = new MyNode(nodeRsp.NodeList[i]);
					node.setParent(nodeStub);
					nodeStub.getListModel().addNode(node, false);
				}
			} else {
				if (isThisNode) {
					//表示是当前节点的子节点，需要添加至子节点中
					node = nodeStub;
					node.setIdlData(nodeRsp.NodeList[i]);

				} else {
					node.setParent(nodeStub);
					nodeStub.getListModel().addNode(node, false);
				}
				node.getListModel().clear(); // 清除关系表
			}
		}
	}

	public SecuResult nodeLoadGroupData(MyNode node, boolean notify) {
		try {
			logger.info("加载组:" + node.getIdlData().NodeNumber);
			SecuResult retResult = new SecuResult();
			GroupQueryRsp groupRsp = new GroupQueryRsp();
			retResult = DataMessageManager.getInstance().groupQueryGroupList(node.getPhysicsNodeNumber(), node.getLogic_NodeNumber(), groupRsp, false, false);
			if (retResult.getRetCode() == 0) {
				nodePushData(node, groupRsp);
			} else {
				groupRsp = new GroupQueryRsp();
				// 触发异步查询
				DataMessageManager.getInstance().groupQueryGroupList_Async(node.getPhysicsNodeNumber(), node.getLogic_NodeNumber());
			}
			return retResult;
		} finally {

		}
	}

	public void nodePushData(MyNode nodeStub, GroupQueryRsp groupRsp) {
		final List<MyGroup> groupTmpList = new ArrayList<MyGroup>();
		final List<MyGroup> groupSingleQueryTmpList = new ArrayList<MyGroup>();
		// 解析组数据
		if (groupRsp != null) {
			for (int i = 0; i < groupRsp.Rows.length; ++i) {
				MyGroup group = groupMap.get(groupRsp.Rows[i].GroupTelno);

				if (group == null) {
					group = new MyGroup(groupRsp.Rows[i], nodeStub.getPhysicsNodeNumber(), nodeStub.getLogic_NodeNumber(), 0);
				} else {
					group.setIdlData(groupRsp.Rows[i]);
					// group.getListModel().clear(); // 清除组中的关系表
				}
				if (!groupTmpList.contains(group)) {
					groupTmpList.add(group);
				}
				

//				System.out.println("组信息!!!!:" + group.getIdlData().GroupTelno + "--" +group.getIdlData().GroupName + " " + group.getIdlData().ParGrpTelno);
			}
		}

		// 建立组的树形关系
		if (groupRsp != null) {
			for (int i = 0; i < groupRsp.Rows.length; ++i) {
				MyGroup group = groupMap.get(groupRsp.Rows[i].GroupTelno);
				if (group == null)
					continue;
				if (group.isTopGroup()) {
					nodeStub.getListModel().addGroup(group, false);
					System.out.println("节点下的组!!!!:" + group.getIdlData().GroupTelno + "--" +group.getIdlData().GroupName);
				} else {
					String[] groupList = groupRsp.Rows[i].ParGrpTelno.split("#");
					boolean isHasParent = false;
					for (String groupNum : groupList) {
						MyGroup parentGroup = groupMap.get(groupNum);
						if (parentGroup != null) {
							parentGroup.getListModel().addGroup(group, false);
							isHasParent = true;
						} else {
						}
					}
					if (!isHasParent) {
						// 父组没有查询上来,将本组作为根节点
						nodeStub.getListModel().addGroup(group, false);

					}
				}
			}
		}
		// 20181126 处理带子组的情况
		{
			for (int i = 0; i < groupRsp.Rows.length; ++i) {
				if (groupRsp.Rows[i].sonGrpTelno.size() > 0) {
					// 设置父节点
					MyGroup parentGroup = groupMap.get(groupRsp.Rows[i].GroupTelno);
					for (Group sonGroup : groupRsp.Rows[i].sonGrpTelno) {
						// 只处理异节点组数据, 其他本组数据已经存在,且已经根据父组号码构建完毕
						if (sonGroup.isOtherNodeGroup()) {
							// 处理节点号码, 利用后三位作为物理节点
							String PhysicsNodeNumber = "";
							String LogicNodeNumber = "";
							if (sonGroup.NodeNumber.length() == 3) {
								PhysicsNodeNumber = sonGroup.NodeNumber;
								LogicNodeNumber = sonGroup.NodeNumber;
							} else {
								PhysicsNodeNumber = sonGroup.NodeNumber.substring(sonGroup.NodeNumber.length() - 3);
								LogicNodeNumber = sonGroup.NodeNumber;
							}
							MyGroup group = groupMap.get(sonGroup.GroupTelno);
							if (group == null) {
								group = new MyGroup(sonGroup, PhysicsNodeNumber, LogicNodeNumber, 0);
							} else {
								// 可能已经存在, 那么就是设置该组为异节点组, 因为可能在以前节点中已经查上来了.
								group.otherNodeGroupSet(sonGroup);
							}
							if (parentGroup != null) {
								parentGroup.getListModel().addGroup(group, false);
							} else {
								;
							}
							// 加入查询子组临时列表
							if (!groupTmpList.contains(group)) {
								groupTmpList.add(group);
							}
							if (!groupSingleQueryTmpList.contains(group)) {
								groupSingleQueryTmpList.add(group);
							}
						}
					}
				}
			}
		}

		// 20181023 异步加载组内成员数据
		{
			Callable<Object> call = new Callable<Object>() {
				public Object call() throws Exception {
					try {
						for (MyGroup group : groupTmpList) {
							Thread.sleep(10);
							group.loadGroupUser(false, false);
						}
						for (MyGroup group : groupSingleQueryTmpList) {
							Thread.sleep(10);
							// 根据组号码查询组的基本信息
							DataMessageManager.getInstance().groupQueryGroup_Single(group.getPhysicsNodeNumber(), group.getLogicNodeNumber(), group.getTelno());
							group.loadGroupUser(false, false);
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
					return null;
				}
			};
			ThreadUtil.getInstance().Call(call);
		}
	}

	public void clear() {
		try {
			nodeMap.clear();
			groupMap.clear();
			userMap.clear();

			rootModel = new RootModel();

			loginNode.getListModel().clear();
			loginNode = null;
			currentNode.getListModel().clear();
			currentNode = null;

			LoginDataManager.getInstance().clear();
			instance = null;
		} catch (Exception e) {

		}
	}

}
